package com.llb666.e.shop.api.controller.center;

import com.llb666.e.shop.api.controller.BaseController;
import com.llb666.e.shop.api.dto.OrderItemsCommentDto;
import com.llb666.e.shop.api.vo.MyCommentVo;
import com.llb666.e.shop.domain.entity.bo.MyCommentBo;
import com.llb666.e.shop.domain.service.MyCommentsService;
import com.llb666.e.shop.infracore.constants.AppConstant;
import com.llb666.e.shop.infracore.web.BaseResponse;
import com.llb666.e.shop.infracore.web.DataResponse;
import com.llb666.e.shop.infracore.web.PagedGridResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liulb666
 */
@Tag(name = "用户中心评价模块", description = "用户中心评价模块相关接口")
@RestController
@RequestMapping("mycomments")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyCommentsController extends BaseController {

    private final MyCommentsService myCommentsService;

    @Operation(summary = "保存评论列表", description = "保存评论列表", method = "POST")
    @PostMapping("/saveList")
    public BaseResponse saveList(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId,
            @Parameter(name = "orderId", description = "订单id", required = true) @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentDto> commentList) {
        // 判断用户和订单是否关联
        checkUserOrder(userId, orderId);
        // 判断评论内容list不能为空
        if (CollectionUtils.isEmpty(commentList)) {
            return DataResponse.errorMsg("评论内容不能为空！");
        }
        myCommentsService.saveComments(orderId, userId, commentList);
        return BaseResponse.ok();
    }

    @Operation(summary = "查询我的评价", description = "查询我的评价", method = "POST")
    @PostMapping("/query")
    public BaseResponse query(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId,
            @Parameter(name = "page", description = "查询下一页的第几页") Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数") Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return DataResponse.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = AppConstant.COMMON_PAGE_SIZE;
        }
        PagedGridResult<MyCommentBo> grid = myCommentsService.queryMyComments(userId, page, pageSize);
        return DataResponse.ok(MyCommentVo.Converter.INSTANCE.bo2VoPage(grid));
    }
}
